package com.apex.lottery.controller.converter;

import com.apex.lottery.common.errorcode.ControllerErrorCodeConstants;
import com.apex.lottery.common.exception.ControllerException;
import com.apex.lottery.controller.result.CreateActivityResult;
import com.apex.lottery.controller.result.FindActivityListResult;
import com.apex.lottery.controller.result.GetActivityDetailResult;
import com.apex.lottery.service.dto.ActivityDTO;
import com.apex.lottery.service.dto.ActivityDetailDTO;
import com.apex.lottery.service.dto.CreateActivityDTO;
import com.apex.lottery.service.dto.PageListDTO;

import java.util.Comparator;
import java.util.stream.Collectors;

/**
 * @ClassName ActivityConverter
 * @Description  controller层转换方法
 * @Author ZJX
 * @Date 2025/4/19 17:20
 * @Version 1.0
 **/
public class ActivityConverter {
    public static CreateActivityResult convertToCreateActivityResult(CreateActivityDTO activity) {
        if (activity == null){
            throw new ControllerException(ControllerErrorCodeConstants.CREATE_ACTIVITY_ERROR);
        }

        CreateActivityResult result = new CreateActivityResult();
        result.setActivityId(activity.getActivityId());

        return result;
    }

    public static FindActivityListResult convertToFindActivityListResult(PageListDTO<ActivityDTO> activityList) {
        if (activityList == null){
            throw new ControllerException(ControllerErrorCodeConstants.FIND_ACTIVITY_LIST_ERROR);
        }

        FindActivityListResult result = new FindActivityListResult();
        result.setTotal(activityList.getTotal());
        result.setRecords(activityList.getRecords().stream()
                        .map(activityDTO -> {
                            FindActivityListResult.ActivityInfo activityInfo = new FindActivityListResult.ActivityInfo();
                            activityInfo.setActivityId(activityDTO.getActivityId());
                            activityInfo.setActivityName(activityDTO.getActivityName());
                            activityInfo.setDescription(activityDTO.getDescription());
                            activityInfo.setValid(activityDTO.valid());
                            return activityInfo;
                        }).collect(Collectors.toList())
        );
        return result;
    }

    public static GetActivityDetailResult convertToGetActivityDetailResult(ActivityDetailDTO detailDTO){
        if (detailDTO == null){
            throw new ControllerException(ControllerErrorCodeConstants.GET_ACTIVITY_DETAIL_ERROR);
        }
        GetActivityDetailResult result = new GetActivityDetailResult();
        result.setActivityId(detailDTO.getActivityId());
        result.setActivityName(detailDTO.getActivityName());
        result.setDescription(detailDTO.getDesc());
        result.setValid(detailDTO.valid());

        // 抽奖顺序：一等奖、二、三
        result.setPrizes(
                detailDTO.getPrizeDTOList().stream()
                        .sorted(Comparator.comparingInt(prizeDTO -> prizeDTO.getTiers().getCode()))
                        .map(prizeDTO -> {
                            GetActivityDetailResult.Prize prize = new GetActivityDetailResult.Prize();
                            prize.setPrizeId(prizeDTO.getPrizeId());
                            prize.setName(prizeDTO.getName());
                            prize.setImageUrl(prizeDTO.getImageUrl());
                            prize.setPrice(prizeDTO.getPrice());
                            prize.setDescription(prizeDTO.getDescription());
                            prize.setPrizeTierName(prizeDTO.getTiers().getMessage());
                            prize.setPrizeAmount(prizeDTO.getPrizeAmount());
                            prize.setValid(prizeDTO.valid());
                            return prize;
                        }).toList()
        );
        result.setUsers(
                detailDTO.getUserDTOList().stream()
                .map(userDTO -> {
                    GetActivityDetailResult.User user = new GetActivityDetailResult.User();
                    user.setUserId(userDTO.getUserId());
                    user.setUserName(userDTO.getUserName());
                    user.setValid(userDTO.valid());
                    return user;
                }).toList()

        );
        return result;
    }
}
